import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import org.apache.poi.ss.formula.functions.T;
import org.junit.Test;

import java.util.List;

/**
 * @program: guli_parent
 * @description:
 * @author: @why
 * @create: 2021-02-05 13:34
 **/
public class testVod {

    /**
     * 初始化操作
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     * @throws ClientException
     */
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        // 点播服务接入区域
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }


    /*获取播放地址函数*/
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId("视频ID");
        return client.getAcsResponse(request);
    }


    @Test
    public  void GetPlayAuthInfo() throws ClientException {
        //创建初始化对象
        DefaultAcsClient client = initVodClient("LTAI4GFzfZNL5ztD7KApT4rf",
                "yFSiXna01tQ0F7hBeGHxUQuLIVA7OO");
        //获取request和response   加锁的凭证
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        //设置视频id值
        request.setVideoId("17bd2663517d46e8ad8ea867f4f9a526");
        request.setAuthInfoTimeout(200L);
        response = client.getAcsResponse(request);

        System.out.println(response.getPlayAuth());
    }

    public String getPlayUrl() throws ClientException {
        String Url="";
        //1.根据视频id获取视频播放地址,就是之前oss的id和密钥
        //创建初始化对象
        DefaultAcsClient client = initVodClient("LTAI4GFzfZNL5ztD7KApT4rf",
                "yFSiXna01tQ0F7hBeGHxUQuLIVA7OO");
        //获取request和response
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        //向request设置视频id
        request.setVideoId("17bd2663517d46e8ad8ea867f4f9a526");
        //使用初始化的方法传递request获取数据并赋值给响应头
        response = client.getAcsResponse(request);
        List<GetPlayInfoResponse.PlayInfo> list = response.getPlayInfoList();
        for (GetPlayInfoResponse.PlayInfo info : list) {
            System.out.println(info.getPlayURL());
            Url=info.getPlayURL();
        }
        System.out.println(response.getVideoBase().getTitle());
        return Url;

    }




    /**
     * 视频上传
     */
    @Test
    public void testUploadVideo(){

        //1.音视频上传-本地文件上传
        //视频标题(必选)
        String title = "uploadTestVideo";
        //本地文件上传和文件流上传时，文件名称为上传文件绝对路径，如:/User/sample/文件名称.mp4 (必选)
        //文件名必须包含扩展名
        String fileName = "C:\\Users\\yasuo\\Desktop\\谷粒学院\\项目资料\\1-阿里云上传测试视频\\Faster.mp4";
        //本地文件上传,title是文件上传到阿里云的名字，filename是本地上传的文件的路径和名称
        UploadVideoRequest request = new UploadVideoRequest("LTAI4GFzfZNL5ztD7KApT4rf",
                "yFSiXna01tQ0F7hBeGHxUQuLIVA7OO", title, fileName);
        /* 可指定分片上传时每个分片的大小，默认为1M字节 */
        request.setPartSize(1 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数，默认为1，(注：该配置会占用服务器CPU资源，需根据服务器情况指定）*/
        request.setTaskNum(1);
    /* 是否开启断点续传, 默认断点续传功能关闭。当网络不稳定或者程序崩溃时，再次发起相同上传请求，可以继续未完成的上传任务，适用于超时3000秒仍不能上传完成的大文件。
        注意: 断点续传开启后，会在上传过程中将上传位置写入本地磁盘文件，影响文件上传速度，请您根据实际情况选择是否开启*/
        request.setEnableCheckpoint(false);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {//这个不是用来判断是否上传成功，而是判断有无返回值
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
 /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }

    }
}